<?php
/*
 * 发型师类
 */
namespace Home\Logic;

class BarberLogic{

    private $pre = 'meiyehong';

    public function __construct() {
        $this->barber = D('Barber');
        import('Memcachectr');
        $this->m = \Think\Memcachectr::create();
    }

    public function insterBarber($arr){
    	$id = $this->barber->add($arr);
        $this->m->delete('barber_store_'.$arr['store_id']);
    	return $id;
    }
    public function updateBarber($arr){
        $this->m->delete('barber_info_'.$arr['id']);
    	$id = $this->barber->where('id='.$arr['id'])->save($arr);
    	return $id;
    }
    public function getBarber($id){
       $info = $this->m->get('barber_info_'.$id);
        if(!$info){
            $info = $this->barber->where('id='.$id)->find();
            $this->m->set('barber_info_'.$id,$info,true);
        }
    	return $info;
    }
    public function updateBarberStatus($id,$status){
        $data['status'] = $status;
        $this->m->delete('barber_info_'.$id);
        $re = $this->getBarber($id);
        $this->m->delete('barber_store_'.$re['store_id']);
        return $this->barber->where('id='.$id)->save($data);
    }

    public function getBarberByStoreId($store_id){
        $re = $this->m->get('barber_store_'.$store_id);
        if(!$re){
            $re = $this->barber->where(array('store_id'=>$store_id,'status'=>1))->order('create_time desc')->select();
            $this->m->set('barber_store_'.$store_id,$re,true);
        }
        return $re;
    }
    
    public function getBarberIds($store_id){
        $re = $this->getBarberByStoreId($store_id);
        foreach((array)$re as $v){
            $arr[] = $v['id'];
        }
        return $arr;
    }
    
    public function getBarberByStoreIds($ids){
        if($ids == NULL || count($ids)<1){
            return ;
        }
        $where = 'store_id in('.implode(',', (array)$ids).') and status=1';
        return $this->barber->where($where)->order('create_time desc')->select();
    }
    
    public function getBarberByMobile($mobile){
        $info = $this->barber->where('mobile='.$mobile.' and status=1')->find();
    	return $info;
    }

    public function level(){
        $re = $this->m->get('barber_level_all');
        if(!$re){
            $l =D('level');
            $re = $l->select();
            $this->m->set('barber_level_all',$re,true);
        }
        return $re;
    }
}


